import data from "./data.mjs";

import { isArray, isObject, isNumber, isWord } from "./utils/CheckType.mjs";
import List from "./utils/List.mjs";

/*


//编写的内置方法=============================================================
//【拓展】创建表的语句需要定义的字段
var getCreateTableSql = function (props) {
  var sql = "";
  if (isNumber(props)) {
    sql = "`" + props + "` int(11) NOT NULL,\n";
  } else {
    sql = "`" + props + "` varchar(255) NOT NULL,\n";
  }
  return sql;
};
//【拓展】创建表的语句需要定义的字段
var getInsertWord = function (word) {
  if (isNumber(word)) {
    return word;
  } else {
    return "'" + word + "'";
  }
};
var getTableSql = function (createSql, name) {
  var sql1 = "DROP TABLE IF EXISTS `" + name + "`;\n";
  var sql2 = "CREATE TABLE `" + name + "` (\n";
  var sql3 = "`id` int(11) NOT NULL AUTO_INCREMENT,\n";
  var sql4 = createSql;
  var sql5 = "`remark` varchar(255) DEFAULT NULL,\n";
  var sql6 = "PRIMARY KEY (`id`)\n";
  var sql7 = ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
  var sql = sql1 + sql2 + sql3 + sql4 + sql5 + sql6 + sql7;
  console.log(sql + "\n");
  return sql;
};

var indexArrNum = 0; //记录插入的数量
var getInsertSql = function (allPropsArr, obj, tableName) {
  var insertSql = "";
  allPropsArr.forEach((item, index, arr) => {
    if (!isArray(obj[item])) {
      insertSql += getInsertWord(obj[item]) + ", ";
    } else {
      insertSql += indexArrNum + ", ";
      hasArray = true;
    }
  });
  var sql1 = insertSql;
  var sql2 =
    "INSERT INTO `" + tableName + "` VALUES (NULL, " + sql1 + "'暂无备注');";
  console.log(sql2);
  return sql2;
};

//内部变量
var createSql = "";
var allPropsArr = [];
var okCreateTable = true;
var upTableName = "";
var hasArray = false;
var isFirst = true;
//执行初始化数据
var init = function () {
  allPropsArr = [];
};

var dealObject = function (obj, tableName) {
  init();
  if (isObject(obj)) {
    for (var props in obj) {
      createSql += getCreateTableSql(props);
      allPropsArr.push(props);
    }
    //得到当前的建表语句
    if (okCreateTable) {
      console.log("#==========================================");
      console.log("# TableName : " + tableName);
      console.log("#==========================================");
      getTableSql(createSql, tableName);
      okCreateTable = false;
    }
    //得到当前的插入数据
    indexArrNum++;
    getInsertSql(allPropsArr, obj, tableName);
  }
};

var arrObj = data.goods;

/*
if (isArray(arrObj)) {
  arrObj.forEach((obj, index, arr) => {
    var id = index;
    //拿到一个元素对其建表
    dealObject(obj, "goods");
    return;
  });
}


var startSeal = function (data) {
  var tableList = new List(); //最大的容器类
  //(1).扫描data的第一层
  if (isObject(data)) {
    for (var propsName in data) {
      tableList.add(propsName);
    }
    //(2).扫描data的第二层
    for (let index = 0; index < tableList.size(); index++) {
      var propsName = tableList.get(index);
      var objOrArr = data[propsName];
      if (isArray(objOrArr)) {
        console.log("数组:" + propsName);
      }
      if (isObject(objOrArr)) {
        startSeal(objOrArr);
      }
    }
  }
};

*/
//根据object遍历数组
var upData = {};
var showArr = function (data, layer) {
  for (var propsName in data) {
    var obj = data[propsName];
    if (isArray(obj) || isObject(obj)) {
      if (isArray(obj)) {
        console.log("[数组]" + "层级=" + layer + " 表名称:" + propsName);
      }

      showArr(obj, layer);
    } else {
      // console.log("[属性]:" + propsName);
    }
  }
};

showArr(data, 1);
